var tsc = require('./tsc.js')
import blueTooth from '@/common/blueTooth.js'
export default {
	//打印的数据
	senBlData(deviceId, serviceId, characteristicId, uint8Array, val) {
		var uint8Buf = Array.from(uint8Array);

		function split_array(datas, size) {
			var result = {};
			var j = 0
			for (var i = 0; i < datas.length; i += size) {
				result[j] = datas.slice(i, i + size)
				j++
			}
			return result
		}
		var sendloop = split_array(uint8Buf, 20);

		function realWriteData(sendloop, i) {
			var data = sendloop[i]
			if (typeof(data) == "undefined") {
				return
			}
			var buffer = new ArrayBuffer(data.length)
			var dataView = new DataView(buffer)
			for (var j = 0; j < data.length; j++) {
				dataView.setUint8(j, data[j]);
			}
			uni.writeBLECharacteristicValue({
				deviceId,
				serviceId,
				characteristicId,
				value: buffer,
				success(res) {
					realWriteData(sendloop, i + 1);
					// uni.hideNavigationBarLoading() //关闭加载动画
				},
				fail(res) {
					let that = this
					that.connectedDevices = uni.getStorageSync('blueToothConnectedDevices')
					if (that.connectedDevices.length > 0) {
						that.connectedDevices.forEach((item, index) => {
							if (item.deviceId == deviceId)
								uni.showLoading({
									title: '蓝牙连接中'
								});
							// setTimeout(() => {
							blueTooth.openBluetoothAdapter().then(() => {
								blueTooth.createBLEConnection({
									item: item
								}).then((res) => {
									blueTooth.getBLEServices({
										item: item
									}).then((res) => {
										let _this = this
										uni.showToast({
											title: "打印中，请稍等",
										})
										realWriteData(sendloop, i +
											1);
									})
								})
								// }, 5000)
							}).catch(() => {
								blueTooth.bluetoothAdapter().then(() => {
									that.connectedDevices.forEach((item, index) => {
										if (item.deviceId == deviceId)
											uni.showLoading({
												title: '蓝牙连接中'
											});
										setTimeout(() => {
											blueTooth
												.openBluetoothAdapter()
												.then(
													() => {
														blueTooth
															.createBLEConnection({
																item: item
															}).then((
																res
															) => {
																blueTooth
																	.getBLEServices({
																		item: item
																	})
																	.then(
																		(
																			res
																		) => {
																			let _this =
																				this
																			uni.showToast({
																				title: "打印中，请稍等",
																			})
																			realWriteData
																				(
																					sendloop,
																					i +
																					1
																				);
																		}
																	)
															})
													})
										}, 2500)
									})
								})
							})
						})
					}
				}
			})
		}
		var i = 0;
		realWriteData(sendloop, i);
	},
	// 打印
	print(val) {
		let deviceId = uni.getStorageSync('blueToothDeviceId')
		let serviceId = uni.getStorageSync('blueToothServiceId')
		let characteristicId = uni.getStorageSync('blueToothCharacteristicId')
		if (deviceId && serviceId && characteristicId) {
			uni.showToast({
				title: "打印中，请稍等",
				icon: "none",
				duration: 3000
			})
			var command = tsc.jpPrinter.createNew()
			val.forEach(e => {
				command.setSize(70, 50)
				command.setGap(2)
				command.setCls()

				command.setText(30, 20, "TSS24.BF2", 1, 1, '物料名称：')
				command.setText(150, 20, "TSS24.BF2", 1, 1, e.materialName)

				command.setText(30, 70, "TSS24.BF2", 1, 1, '物料规格：')
				command.setText(150, 70, "TSS24.BF2", 1, 1, e.materialSpec)

				command.setText(30, 120, "TSS24.BF2", 1, 1, '色光号：')
				command.setText(150, 120, "TSS24.BF2", 1, 1, e.sgh)

				command.setText(30, 170, "TSS24.BF2", 1, 1, '数量：')
				command.setText(150, 170, "TSS24.BF2", 1, 1, e.num)

				command.setText(30, 220, "TSS24.BF2", 1, 1, '颜色：')
				command.setText(150, 220, "TSS24.BF2", 1, 1, e.color)

				command.setText(30, 270, "TSS24.BF2", 1, 1, '合同号：')
				command.setText(150, 270, "TSS24.BF2", 1, 1, e.htdocnum)

				command.setText(30, 320, "TSS24.BF2", 1, 1, '仓库名称：')
				command.setText(150, 320, "TSS24.BF2", 1, 1, e.warehouseName)
				
				command.setText(30, 370, "TSS24.BF2", 1, 1, '花型名称：')
				command.setText(150, 370, "TSS24.BF2", 1, 1, e.hxmc)

				command.setQR(355, 105, "L", 8, "A", e.barcode)
				command.setText(355, 300, "TSS24.BF2", 1, 1, e.barcode)
				command.setPagePrint()

			})
			this.senBlData(deviceId, serviceId, characteristicId, command.getData(), val)
		} else {
			uni.showToast({
				title: "请连接蓝牙！",
				icon: "error",
				duration: 3000
			})
		}
	},
}